<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="open('3')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="creator" label="作者" width="180">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>

        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="open('1', row.id)"></i>
              <i class="el-icon-edit-outline" @click="open('2', row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </div>
    <el-drawer :title="drawerTitle" :visible.sync="drawer" size="50%">
      <div v-if="drawerType === '1'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form
        v-else
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticle,
  addArticle,
  delArticle,
  getDetail,
  updateArticle
} from '@/api/article'

// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      tableData: [],
      current: 1,
      pageSize: 10,
      total: 0,
      drawer: false,
      drawerType: '',
      form: { stem: '', content: '' },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getBook()
  },
  methods: {
    del (id) {
      this.$confirm('你确定要删除吗？', '提示')
        .then(async () => {
          // 说明点击了确认
          await delArticle({ id })
          this.$message.success('删除成功')
          this.getBook()
        })
        .catch(() => {
          // 说明点击了取消
        })
    },
    async getBook () {
      const res = await getArticle({
        current: this.current,
        pageSize: this.pageSize
      })

      this.tableData = res.data.rows
      this.current = res.data.current
      this.pageSize = res.data.pageSize
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.getBook()
    },
    handleCurrentChange (val) {
      this.current = val
      this.getBook()
    },
    async open (type, id) {
      this.drawer = true
      this.drawerType = type
      if (id) {
        const res = await getDetail({ id })
        // console.log(res)
        this.form = { ...res.data }
      }
    },
    async submit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.drawerType === '3') {
            const { stem, content } = this.form
            await addArticle({ stem, content })
            this.$message.success('添加成功')
            this.current = 1
          }
          if (this.drawerType === '2') {
            const { stem, content, id } = this.form
            await updateArticle({ stem, content, id })
            this.$message.success('添加成功')
            this.current = 1
          }

          this.getBook()
          this.handleClose()
        }
      })
    },
    handleClose () {
      this.$refs.form?.resetFields()
      this.isShowDrawer = false
    }
  },
  computed: {
    drawerTitle () {
      let title = '大标题'
      if (this.drawerType === '3') title = '添加面经'
      if (this.drawerType === '1') title = '面经预览'
      if (this.drawerType === '2') title = '修改面经'
      return title
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
